package com.x.example;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.x.uikit.service.IPageRouteService;
import com.x.example.demo.MainPage;
import com.example.uiframework.utils.Utils;
import com.x.uikit.page.XPage;
import com.x.uikit.page.XPageManager;
import com.x.uikit.page.XPageManagerConfig;
import com.x.uikit.router.IXRouteHandler;
import com.x.uikit.router.XRoute;
import com.x.uikit.service.XService;

public class MainActivity extends AppCompatActivity {
    XPageManager mPageManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.theme.setImmerseStatusBar(this);
        Utils.theme.setStatusTextColor(true, this);
        getSupportActionBar().hide();
        MainPage page = new MainPage(this);
        XPageManagerConfig config = XPageManagerConfig.obtainBuilder()
                .setActivity(this)
                .setFitsSystemWindows(true)
                .setRootPage(page)
                .build();
        mPageManager = new XPageManager(config);
        RouterManager.get().registerRouteHandler(mRouteHandler);
        handleRouteFromIntent(getIntent());
        XService.register(IPageRouteService.class, new IPageRouteHandleServiceImpl());
    }

    private class IPageRouteHandleServiceImpl implements IPageRouteService {

        @Override
        public void navigate(@NonNull XRoute route, @NonNull PageLoader loader) {
            XPage page = loader.load(route, MainActivity.this);
            if (page != null) {
                mPageManager.push(page);
            }
        }
    }

    private final IXRouteHandler mRouteHandler = new IXRouteHandler() {
        @Override
        public boolean needIntercept(@NonNull XRoute route) {
            return PageRouteList.hasPath(route);
        }

        @Override
        public void onHandle(@NonNull XRoute route) {
            PageRouteList.navigate(route, mPageManager);
        }
    };

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleRouteFromIntent(intent);
    }

    private void handleRouteFromIntent(Intent intent) {
        if (intent == null) {
            return;
        }
        XRoute route = intent.getParcelableExtra("route");
        if (route != null) {
            mRouteHandler.onHandle(route);
        }
    }


    @Override
    protected void onStart() {
        super.onStart();
        mPageManager.getLifecycle().onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPageManager.getLifecycle().onResume();
    }

    @Override
    protected void onPause() {
        mPageManager.getLifecycle().onPause();
        super.onPause();
    }

    @Override
    protected void onStop() {
        mPageManager.getLifecycle().onStop();
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mPageManager.getLifecycle().onDestroy();
        RouterManager.get().unregisterRouteHandler(mRouteHandler);
        super.onDestroy();
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (mPageManager.dispatchKeyEvent(event)) {
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

    @Override
    public void onBackPressed() {
        boolean handle = mPageManager.onBackPressed();
        if (!handle) {
            super.onBackPressed();
        }
    }
}